<?php


namespace App\Utils\JWT;


class RawData extends DataGet
{
    public string $rawString;

    public function __construct(array $data=[])
    {
        if ($data){
            $this->append($data);
        }
    }

    public function toString()
    {
        if (!empty($this->rawString)){
            return $this->rawString;
        }
        $this->rawString = Signer::encode($this->data);
        return $this->rawString;
    }

    public static function parse(string $rawString)
    {
        $parse = Signer::decode($rawString);
        if (!$parse){
            throw new \Exception('JWT Error');
        }
        $ins = new static($parse);
        $ins->rawString = $rawString;
        return $ins;
    }
}
